package servlet.events;
import java.io.IOException;
//import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bundle.ErrorsBundleKey.UserErrorBundleKey;

import util.FormValidation;

import dao.DAOFactory;
import model.User;

public class EditUserHandler extends EventHandler {
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		if (validateForm(request)) 
		{
			User user = getUser(request);
			
			if (DAOFactory.getUserDAO().update(user)) {
				HttpSession session = (HttpSession) request.getSession();
				request.setAttribute("status", "ok");
				session.setAttribute("user", user);				
			}
			else {
				request.setAttribute("status", "error");
				setErrors(UserErrorBundleKey.UPDATE_USER_FAIL, request);
			}
		}
		forward(urlBundle.getString("EDITUSER_URL"), request, response);
	}
	
	private boolean validateForm(HttpServletRequest request) {
		int i = 0;
//		ResourceBundle bundle = ResourceBundle.getBundle("Errors");
		
		if(request.getParameter("lastname") == null ||
				request.getParameter("lastname").trim().equals("")) {
//			request.setAttribute("LASTNAME_NOT_NULL", bundle.getString("LASTNAME_NOT_NULL"));
			setErrors(UserErrorBundleKey.LASTNAME_NOT_NULL, request);
			i++;
		}
		
		if(request.getParameter("firstname") == null ||
				request.getParameter("firstname").trim().equals("")) {
//			request.setAttribute("FIRSTNAME_NOT_NULL", bundle.getString("FIRSTNAME_NOT_NULL"));
			setErrors(UserErrorBundleKey.FIRSTNAME_NOT_NULL, request);
			i++;
		}
		
		if(request.getParameter("email") == null ||
				request.getParameter("email").trim().equals("")) {
//			request.setAttribute("EMAIL_NOT_NULL", bundle.getString("EMAIL_NOT_NULL"));
			setErrors(UserErrorBundleKey.EMAIL_NOT_NULL, request);
			i++;
		}
		//else if (!user.getEmail().matches("^[a-zA-Z0-9]+([-+.'][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*\.[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*$") {
		else if(!FormValidation.validateEmail(request.getParameter("email"))){
//			request.setAttribute("EMAIL_INVALID", bundle.getString("EMAIL_INVALID"));
			setErrors(UserErrorBundleKey.EMAIL_INVALID, request);
			i++;
		}
		
		if(request.getParameter("address") == null ||
				request.getParameter("address").trim().equals("")) {
//			request.setAttribute("ADDRESS_NOT_NULL", bundle.getString("ADDRESS_NOT_NULL"));
			setErrors(UserErrorBundleKey.ADDRESS_NOT_NULL, request);
			i++;
		}
		
		if (i != 0)
			return false;
		
		return true;
	}	
	
	private User getUser(HttpServletRequest request) {
		HttpSession session = (HttpSession) request.getSession();
		
		//get user info from session 'user'
		User user = new User((User) session.getAttribute("user"));
		
		//fields that are able to be changed
		user.setFirstname(request.getParameter("firstname"));
		user.setLastname(request.getParameter("lastname"));
		user.setEmail(request.getParameter("email"));
		user.setAddress(request.getParameter("address"));
		user.setGender(Integer.parseInt(request.getParameter("gender")));

		return user;
	}	
}
